package com.scalar.chiptrack.wipProcessView;

import javax.swing.*;
import javax.swing.text.html.parser.ParserDelegator;
import java.awt.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;

/**
 * User: Sujan
 * Date: May 4, 2005
 * Time: 11:48:52 AM
 */
public class ProcessViewApplet extends JApplet
{
	GraphViewPanel gvp 					= null;
	ProcessViewGraph processViewGraph 	= null;
	URL url 			= null;
	String contextURL 	= null;
	String productId 	= null;
	public void init ()
	{
		/*
		 * The following line is the workaround for the jre: 1.6.0_24 issue.
		  * see: http://stackoverflow.com/questions/4330602/html-no-longer-working-in-jlabel-and-other-components
		   * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6993691
		 */
		ParserDelegator workaround = new ParserDelegator();
		setVisible ( true );
		contextURL 	= getParameter( "contextURL" );
		productId	= getParameter ( "productId" );
		URLConnection con = null;
		ObjectInputStream inputToApplet = null;
		ObjectOutputStream outStream = null;
		try
		{
			url = new URL ( contextURL + "/wipProcessFlowViewAction.do" );
			con = url.openConnection();
			if ( !con.getDoOutput() )
			{
				con.setDoOutput( true );
			}
			con.setDoInput( true );
			con.setUseCaches ( false );
			con.setDefaultUseCaches ( false );
			con.setAllowUserInteraction( true );
			con.setRequestProperty ("Content-Type", "application/octet-stream");
			con.connect();

			outStream = new ObjectOutputStream ( con.getOutputStream () );
			outStream.writeObject ( productId );
			outStream.flush ();
			outStream.close();

			inputToApplet = new ObjectInputStream( con.getInputStream() );
			try
			{
				processViewGraph = ( ProcessViewGraph )inputToApplet.readObject();
			}
			catch ( ClassNotFoundException e2 )
			{
				e2.printStackTrace();
			}
			inputToApplet.close();
		}
		catch ( MalformedURLException e )
		{
			e.printStackTrace();
		}
		catch ( IOException e )
		{
			e.printStackTrace();
		}
		finally
		{
			//CLOSE ALL OPEN STREAMS.
			if ( inputToApplet != null )
			{
				try { inputToApplet.close (); } catch ( Exception ignore ) {}
			}
			if ( outStream != null )
			{
				try { outStream.close(); } catch ( Exception ignore ) {}
			}
            if ( con != null )
            {
                con = null;
            }
        }
		gvp = new GraphViewPanel ( processViewGraph );
		double leftGap = ( ( double )this.getBounds().width - gvp.getPreferredSize ().getWidth () ) / 2;
		JPanel mainPanel = new JPanel ( new BorderLayout ( ) );
		mainPanel.setBackground ( Color.white );
		JPanel leftPanel = new JPanel ();
		leftPanel.setBackground ( Color.white );
		JPanel rightPanel = new JPanel ();
		rightPanel.setBackground ( Color.white );
        leftGap = leftGap - 10;
		if ( leftGap > 0 )
		{
			leftPanel.setPreferredSize ( new Dimension ( ( int )leftGap, this.getBounds().height ));
			rightPanel.setPreferredSize ( new Dimension ( ( int )leftGap, this.getBounds().height ));
		}
		else
		{
			leftPanel.setVisible ( false );
			rightPanel.setVisible ( false );
		}

		mainPanel.add ( leftPanel, BorderLayout.LINE_START );
		mainPanel.add ( rightPanel ,BorderLayout.LINE_END );
		mainPanel.add ( gvp, BorderLayout.CENTER );
		JScrollPane scrollPane = new JScrollPane( mainPanel );
		JViewport port = scrollPane.getViewport();
		port.setScrollMode( JViewport.SIMPLE_SCROLL_MODE );
		getContentPane ().add ( scrollPane );
	}

	public void start ()
	{
		gvp.repaint();
	}
}
